package com.example.demo.controller;


import com.example.demo.model.Book;
import com.example.demo.model.User;
import com.example.demo.service.BookCacheService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.List;

/**
 * 功能描述：
 *
 * @Author: Zhanghf
 * @Date: 2021/5/20 17:45
 */
//如何设置数据表字段不能重复
@Controller
public class IndexController {
    @Autowired
    private BookCacheService bookCacheService;
    @GetMapping("index")
    public String index(HttpServletResponse response, HttpServletRequest request, Model model, HttpSession session) throws IOException {
        User user= (User) session.getAttribute("user");
        model.addAttribute("user",user);
        List<Book> indexGoodRec = bookCacheService.indexGoodRec();
        //本周强推、精品推荐都是根据收藏量来进行推荐
        //本周强推
        List<Book> indexList = indexGoodRec.subList(0, 4);
        //精品推荐
        List<Book> goodRec = indexGoodRec.subList(4, 13);
        //热门推荐（根据点击量来推荐）
        List<Book> topxuanhuan = bookCacheService.hitsRank("玄幻奇幻");
        List<Book> topdushi =  bookCacheService.hitsRank("都市言情");
        List<Book> topwuxia =  bookCacheService.hitsRank("仙侠武侠");
        List<Book> toptongren = bookCacheService.hitsRank("同人网游");
        List<Book> topchuanyue = bookCacheService. hitsRank("穿越历史");
        List<Book> topkehuan = bookCacheService.hitsRank("科幻灵异");
        List<Book> latestupdate = bookCacheService.indexLatestUpdate();   //最近更新
        List<Book> latestover = bookCacheService.indexLatestOver(); //最近完本
        model.addAttribute("topxuanhuan", topxuanhuan)
                .addAttribute("topdushi", topdushi)
                .addAttribute("topkehuan", topkehuan)
                .addAttribute("topwuxia", topwuxia)
                .addAttribute("toptongren", toptongren)
                .addAttribute("topchuanyue", topchuanyue);
        model.addAttribute("goodRec",goodRec);
        model.addAttribute("indexList",indexList);
        model.addAttribute("latestupdate",latestupdate);
        model.addAttribute("latestover",latestover);
        return "index";
    }
}
